2017-01-20 24 views
0

我从资产文件夹播放MP3时遇到问题。当我尝试改变按钮图像,MP3停止播放(不总是...) 错误,即时通讯越来越Android MediaPlayer在资产按钮上更改图像后停止播放表单资产

W/MediaPlayer的-JNI:MediaPlayer的没有被释放完成

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    if(enableMusic){ 
     playMusic(); 
    } 
... 


private void playMusic() { 
    boolean noAudioFile = false; 
    Context context = this; // or ActivityNotification.this 
    MediaPlayer mpMusic = new MediaPlayer(); 
    AssetManager mg2 = getResources().getAssets(); 
    String musicPath = langFolder+audioFolder+"system/Bubble_Bath.mp3"; 
    try { 
     mg2.open(musicPath); 

    } catch (IOException ex) { 
     noAudioFile = true; 
    } 

    if(!noAudioFile) { 
     AssetFileDescriptor descriptor = null; 
     try { 
      descriptor = context.getAssets().openFd(musicPath); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     long start = descriptor.getStartOffset(); 
     long end = descriptor.getLength(); 
     try { 
      mpMusic.setDataSource(descriptor.getFileDescriptor(), start, end); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    else{ 

    } 
    mpMusic.setVolume(5,5); 
    try { 
     mpMusic.prepare(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    mpMusic.start(); 
    mpMusic.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
     @Override 
     public void onCompletion(MediaPlayer mp) { 
      mp.start(); 
     } 
    }); 


} 


public void nextCard(View view) throws IOException { 
    i++; 
    if (i==files.length) i=0; 
    if(i>-1){ 
     Button btnNext = (Button)findViewById(R.id.btnNext); 
     btnNext.setText("Next"); 
    } 
    Drawable d = Drawable.createFromStream(getAssets().open(categoryPath+"/"+files[i]+".png"), null); 
    findViewById(R.id.btnImage).setBackgroundDrawable(d); 
    if(files[i].substring(0,1).contains("a") || files[i].substring(0,1).contains("i")){ 
     startFileSound = langFolder + audioFolder + "system/GAME_thisisan.mp3"; 
    } 
    else if (files[i].substring(files[i].length()-1,files[i].length()).contains("s")){ 
     startFileSound = langFolder + audioFolder + "system/GAME_theseare.mp3"; 
    } 
    else{ 
     startFileSound = noAudioFileSound; 
    } 
} 

当我尝试评论下面的行时,mp3播放没有问题。

Drawable d = Drawable.createFromStream(getAssets()。open(categoryPath +“/”+ files [i] +“。png”),null);

回答

0

您正在加载绘制,这是正确的。但不要在主线程中做。尝试在不同的线程中执行此操作。

+0

在另一个线程开始音乐帮助:) 感谢您的建议 '线程的线程=新主题(){ @覆盖 公共无效的run(){ INT P = 0; (p!= 1){ mpMusic.start(); } } }; thread.start(); ' –

0

永远不要创建MediaPlayer就像从资产文件夹此 MediaPlayer mpMusic = new MediaPlayer();

使用

 MediaPlayer.create(context,R.raw.music); 
+0

我想用资产的文件夹。这是唯一的方法,我可以找到使用资产文件夹 –

+0

@RobertRupa你尝试加载'位图'在另一个线程?也许'AsyncTask'? – Ekalips

+0

我需要首先学习:)。 我会尽力的。 –

0

尽量接近描述

mpMusic.setDataSource(descriptor.getFileDescriptor(), start, end); 
descriptor.close(); 
+0

关闭描述符后,问题仍然存在。 –