- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在这里阅读了几篇建议使用单例代码的文章,我已经阅读并放入音板,因为我想使用单例代码只允许一个媒体播放器实例,无论用户定义了多少次点击.
基本上我只想要一种声音,如果用户在播放期间单击另一个按钮,它会停止当前声音并播放按下的声音。我只想要一个 MediaPlayer 实例,但不知道如何实现它。
这是我的代码的基本思想:
package com.example.context;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class main extends Activity implements OnClickListener {
MediaPlayer player;
int[] ressound ={R.raw.boomstick, R.raw.chainsaw, R.raw.shebitch, R.raw.byebye,
R.raw.comegetsome, R.raw.groovy, R.raw.shoelace, R.raw.smart, R.raw.yeahbaby};//added as needed
int j=0;
private static final String TAG = "MyTag";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Coding for all buttons, registers, and Listeners
Button btn1 = (Button) findViewById(R.id.btn1);
registerForContextMenu(btn1);
btn1.setOnClickListener(this);
Button btn2 = (Button) findViewById(R.id.btn2);
registerForContextMenu(btn2);
btn2.setOnClickListener(this);
Button btn3 = (Button) findViewById(R.id.btn3);
registerForContextMenu(btn3);
btn3.setOnClickListener(this);
}
//On click Handlers for multiple buttons
public void onClick(View v) {
switch(v.getId()){
case R.id.btn1:
// action to perform on button 1
j = 0;
playResource();
break;
case R.id.btn2:
// action to perform on button 1
j = 1;
playResource();
break;
case R.id.btn3:
// action to perform on button 1
j = 2;
playResource();
break;
}
public void playResource(int j, int resource) {
this.j = j;
if (player != null) {
if (player.isPlaying())
player.stop();
player.reset();
//from MediaPlayer implementation (link above)
try {
AssetFileDescriptor afd = getResources().openRawResourceFd(resource);
if (afd == null) return;
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
player.prepare();
} catch (IOException ex) {
Log.d(TAG, "create failed:", ex);
// failed: return
} catch (IllegalArgumentException ex) {
Log.d(TAG, "create failed:", ex);
// failed: return
} catch (SecurityException ex) {
Log.d(TAG, "create failed:", ex);
// failed: return
}
}
else {
//player is null
//it will create new MediaPlayer instance, setDataSource and call prepare
player = MediaPlayer.create(this, resource);
}
//if everything ok play file
//in case of any error return from method before (catch)
player.start();
}
这也是我必须更新和更改我的代码的地方,但它给我带来了传递 playresource() 函数的问题。我是不是传错了,传的应该是private吧。
最佳答案
为此您不需要单例。你在做什么是每次你想播放一个文件时调用创建方法。这是错误的,因为您已经拥有 MediaPlayer 实例。检查MediaPlayer.create implementation .我会这样做:
playResource 方法示例:
private static final String TAG = "MyTag";
playResource(int j, int resource) {
this.j = j;
if (player != null) {
if (player.isPlaying())
player.stop();
player.reset();
//from MediaPlayer implementation (link above)
try {
AssetFileDescriptor afd = getResources().openRawResourceFd(resource);
if (afd == null) return null;
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
player.prepare();
} catch (IOException ex) {
Log.d(TAG, "create failed:", ex);
// failed: return
} catch (IllegalArgumentException ex) {
Log.d(TAG, "create failed:", ex);
// failed: return
} catch (SecurityException ex) {
Log.d(TAG, "create failed:", ex);
// failed: return
}
}
else {
//player is null
//it will create new MediaPlayer instance, setDataSource and call prepare
player = MediaPlayer.create(this, resource);
}
//if everything ok play file
//in case of any error return from method before (catch)
player.start();
}
当你不再需要MediaPlayer时,你应该释放它。例如在 onPause() 调用中:
if (player != null) {
player.release();
player = null;
}
我没有测试过,所以可能会有错误。希望对您有所帮助。
关于Android MediaPlayer单例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7831320/
我最近购买了《C 编程语言》并尝试了 Ex 1-8这是代码 #include #include #include /* * */ int main() { int nl,nt,nb;
早上好!我有一个变量“var”,可能为 0。我检查该变量是否为空,如果不是,我将该变量保存在 php session 中,然后调用另一个页面。在这个新页面中,我检查我创建的 session 是否为空,
我正在努力完成 Learn Python the Hard Way ex.25,但我无法理解某些事情。这是脚本: def break_words(stuff): """this functio
我是一名优秀的程序员,十分优秀!