简体   繁体   English

"Android MediaPlayer.Create() 返回 null"

[英]Android MediaPlayer.Create() returns null

I am developing an Android App that plays some sounds.我正在开发一个播放一些声音的 Android 应用程序。 For that I am creating an object of MediaPlayer.为此,我正在创建一个 MediaPlayer 对象。 Below is the code:下面是代码:

mp = MediaPlayer.create(this, R.raw.testSound);

Here mp is null for Android 2.3.3 (API Level 10), i tried with other versions of Android (2.1, 2.2 etc.) it works fine.这里 mp 对于 Android 2.3.3(API 级别 10)为空,我尝试使用其他版本的 Android(2.1、2.2 等)它工作正常。 However with Android 2.3.3 (API Level 10) MediaPlayer.create() call returns null.但是,对于 Android 2.3.3(API 级别 10),MediaPlayer.create() 调用返回 null。

The sound file (testSound.wav) is a wav file.声音文件 (testSound.wav) 是一个 wav 文件。 I tried parsing the wav file to see if it is corrupted or not.我尝试解析 wav 文件以查看它是否已损坏。 It seems just fine.看起来刚刚好。 Also, I could play this sound file using Windows Media Player.另外,我可以使用 Windows Media Player 播放这个声音文件。

Here's the testSound.wav file: testSound.wav and below is the code in detail:这是 testSound.wav 文件: testSound.wav以下是详细代码:

public MediaPlayer mp;

// OnCreate() funciton
mp = MediaPlayer.create(this, R.raw.testSound);
if(mp == null)   // mp is null for Android 2.3.3 on real device and on AVD both
  {
  Toast msg = Toast.makeText(getApplicationContext(), "Could not play sound",         
  Toast.LENGTH_LONG);
  msg.show();
  }

//SetMediaFileToPlay() is called on Click event of button
void SetMediaFileToPlay() 
{       
    AssetFileDescriptor afd;     
    Resources res = getResources();
    Log.d("In SetMediaFileToPlay %s", g_strFocusedImage.toString());
    switch (g_strFocusedImage)
    {
      case RESID_ALPHA1:
      afd = res.openRawResourceFd(R.raw.ik);
      if(mp != null)
      {
        mp.reset();
        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),      
        afd.getLength());
      }  
      break;
      .........  //other cases
   }

 if(mp != null)
  {
    mp.prepare();
    mp.start();
  }

} }

Please help.请帮忙。 Thank You.谢谢。

I am developing an Android App that plays some sounds.我正在开发一个播放一些声音的 Android 应用程序。 For that I am creating an object of MediaPlayer.为此,我正在创建一个 MediaPlayer 对象。 Below is the code:下面是代码:

mp = MediaPlayer.create(this, R.raw.testSound);

Here mp is null for Android 2.3.3 (API Level 10), i tried with other versions of Android (2.1, 2.2 etc.) it works fine.这里 mp 对于 Android 2.3.3(API 级别 10)为 null,我尝试使用其他版本的 Android(2.1、2.2 等)它工作正常。 However with Android 2.3.3 (API Level 10) MediaPlayer.create() call returns null.但是对于 Android 2.3.3(API 级别 10)MediaPlayer.create() 调用返回 null。

The sound file (testSound.wav) is a wav file.声音文件 (testSound.wav) 是一个 wav 文件。 I tried parsing the wav file to see if it is corrupted or not.我尝试解析 wav 文件以查看它是否已损坏。 It seems just fine.好像还好。 Also, I could play this sound file using Windows Media Player.此外,我可以使用 Windows Media Player 播放此声音文件。

Here's the testSound.wav file: testSound.wav and below is the code in detail:这是 testSound.wav 文件: testSound.wav和以下是详细代码:

public MediaPlayer mp;

// OnCreate() funciton
mp = MediaPlayer.create(this, R.raw.testSound);
if(mp == null)   // mp is null for Android 2.3.3 on real device and on AVD both
  {
  Toast msg = Toast.makeText(getApplicationContext(), "Could not play sound",         
  Toast.LENGTH_LONG);
  msg.show();
  }

//SetMediaFileToPlay() is called on Click event of button
void SetMediaFileToPlay() 
{       
    AssetFileDescriptor afd;     
    Resources res = getResources();
    Log.d("In SetMediaFileToPlay %s", g_strFocusedImage.toString());
    switch (g_strFocusedImage)
    {
      case RESID_ALPHA1:
      afd = res.openRawResourceFd(R.raw.ik);
      if(mp != null)
      {
        mp.reset();
        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),      
        afd.getLength());
      }  
      break;
      .........  //other cases
   }

 if(mp != null)
  {
    mp.prepare();
    mp.start();
  }

} }

Please help.请帮忙。 Thank You.谢谢你。

I am developing an Android App that plays some sounds.我正在开发一个播放一些声音的 Android 应用程序。 For that I am creating an object of MediaPlayer.为此,我正在创建一个 MediaPlayer 对象。 Below is the code:下面是代码:

mp = MediaPlayer.create(this, R.raw.testSound);

Here mp is null for Android 2.3.3 (API Level 10), i tried with other versions of Android (2.1, 2.2 etc.) it works fine.这里 mp 对于 Android 2.3.3(API 级别 10)为 null,我尝试使用其他版本的 Android(2.1、2.2 等)它工作正常。 However with Android 2.3.3 (API Level 10) MediaPlayer.create() call returns null.但是对于 Android 2.3.3(API 级别 10)MediaPlayer.create() 调用返回 null。

The sound file (testSound.wav) is a wav file.声音文件 (testSound.wav) 是一个 wav 文件。 I tried parsing the wav file to see if it is corrupted or not.我尝试解析 wav 文件以查看它是否已损坏。 It seems just fine.好像还好。 Also, I could play this sound file using Windows Media Player.此外,我可以使用 Windows Media Player 播放此声音文件。

Here's the testSound.wav file: testSound.wav and below is the code in detail:这是 testSound.wav 文件: testSound.wav和以下是详细代码:

public MediaPlayer mp;

// OnCreate() funciton
mp = MediaPlayer.create(this, R.raw.testSound);
if(mp == null)   // mp is null for Android 2.3.3 on real device and on AVD both
  {
  Toast msg = Toast.makeText(getApplicationContext(), "Could not play sound",         
  Toast.LENGTH_LONG);
  msg.show();
  }

//SetMediaFileToPlay() is called on Click event of button
void SetMediaFileToPlay() 
{       
    AssetFileDescriptor afd;     
    Resources res = getResources();
    Log.d("In SetMediaFileToPlay %s", g_strFocusedImage.toString());
    switch (g_strFocusedImage)
    {
      case RESID_ALPHA1:
      afd = res.openRawResourceFd(R.raw.ik);
      if(mp != null)
      {
        mp.reset();
        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),      
        afd.getLength());
      }  
      break;
      .........  //other cases
   }

 if(mp != null)
  {
    mp.prepare();
    mp.start();
  }

} }

Please help.请帮忙。 Thank You.谢谢你。

I am developing an Android App that plays some sounds.我正在开发一个播放一些声音的 Android 应用程序。 For that I am creating an object of MediaPlayer.为此,我正在创建一个 MediaPlayer 对象。 Below is the code:下面是代码:

mp = MediaPlayer.create(this, R.raw.testSound);

Here mp is null for Android 2.3.3 (API Level 10), i tried with other versions of Android (2.1, 2.2 etc.) it works fine.这里 mp 对于 Android 2.3.3(API 级别 10)为 null,我尝试使用其他版本的 Android(2.1、2.2 等)它工作正常。 However with Android 2.3.3 (API Level 10) MediaPlayer.create() call returns null.但是对于 Android 2.3.3(API 级别 10)MediaPlayer.create() 调用返回 null。

The sound file (testSound.wav) is a wav file.声音文件 (testSound.wav) 是一个 wav 文件。 I tried parsing the wav file to see if it is corrupted or not.我尝试解析 wav 文件以查看它是否已损坏。 It seems just fine.好像还好。 Also, I could play this sound file using Windows Media Player.此外,我可以使用 Windows Media Player 播放此声音文件。

Here's the testSound.wav file: testSound.wav and below is the code in detail:这是 testSound.wav 文件: testSound.wav和以下是详细代码:

public MediaPlayer mp;

// OnCreate() funciton
mp = MediaPlayer.create(this, R.raw.testSound);
if(mp == null)   // mp is null for Android 2.3.3 on real device and on AVD both
  {
  Toast msg = Toast.makeText(getApplicationContext(), "Could not play sound",         
  Toast.LENGTH_LONG);
  msg.show();
  }

//SetMediaFileToPlay() is called on Click event of button
void SetMediaFileToPlay() 
{       
    AssetFileDescriptor afd;     
    Resources res = getResources();
    Log.d("In SetMediaFileToPlay %s", g_strFocusedImage.toString());
    switch (g_strFocusedImage)
    {
      case RESID_ALPHA1:
      afd = res.openRawResourceFd(R.raw.ik);
      if(mp != null)
      {
        mp.reset();
        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),      
        afd.getLength());
      }  
      break;
      .........  //other cases
   }

 if(mp != null)
  {
    mp.prepare();
    mp.start();
  }

} }

Please help.请帮忙。 Thank You.谢谢你。

I noticed on my app that uri's with a space in them,我在我的应用程序中注意到 uri 中有一个空格,
ie: "https://zzzzzzzzzz.amazonaws.com/Moed Katan/MDK-018.mp3"即: “https://zzzzzzzzzz.amazonaws.com/Moed Katan/MDK-018.mp3”
are returning null whereas uri's without spaces work just fine.正在返回 null 而没有空格的 uri 工作得很好。

This is interesting since in a regular browser it automatically replaces the space with "%20" and the file plays.这很有趣,因为在常规浏览器中它会自动将空格替换为“%20”并播放文件。

Any way I can prevent this?有什么办法可以防止这种情况吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM