مشکل کوچیک در کد موزیک زیر
دوستان من تو کد زیر به یه مشکل خوردم که هنگام ران شدن و بالا اومدن برنامه اهنگ بدون زدن دکمه میخونه میخوام این حالت رو از بین ببرم و فقط با زدن دکمه احرا بشه
extends Fragment {
SeekBar seekBar;
MediaPlayer mediaplayer;
Button pause_btn;
Button play_btn;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_games, container, false);
pause_btn = (Button)rootView. findViewById(R.id.pause_btn);
play_btn = (Button)rootView. findViewById(R.id.play_btn);
seekBar = (SeekBar)rootView. findViewById(R.id.slider);
mediaplayer = MediaPlayer.create(getActivity(), R.raw.playmusic);
// ######### az rooye moshakhasate file soti seekbar ro tanzim mikonim...
mediaplayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
private Runnable onEverySecond= new Runnable() {
@Override
public void run() {
if (seekBar != null) {
seekBar.setProgress(mediaplayer.getCurrentPosition());
}
if (mediaplayer.isPlaying()) {
seekBar.postDelayed(onEverySecond, 1000);
}
}
};
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
seekBar.setMax(mediaplayer.getDuration());
seekBar.postDelayed(onEverySecond= new Runnable() {
@Override
public void run() {
if (seekBar != null) {
seekBar.setProgress(mediaplayer.getCurrentPosition());
}
if (mediaplayer.isPlaying()) {
seekBar.postDelayed(onEverySecond, 1000);
}
}
}, 1000);
}
});
// ########## mediaplayer ro start mikonim
mediaplayer.start();
// ############## baraye inke vaghti seekbar ro jabeja mikonid...
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (fromUser) {
// vaghti seek bar ro jabeja mikonim inja ejra mishe...
mediaplayer.seekTo(progress);
}
}
});
//dokmeye pause
pause_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaplayer.pause();
}
});
//dokmeye play
play_btn.setOnClickListener(new View.OnClickListener() {
private Runnable onEverySecond;
@Override
public void onClick(View view) {
mediaplayer.start();
// tanzim e dobareye seekbar bad az inke dobare play kardim
seekBar.setMax(mediaplayer.getDuration());
seekBar.postDelayed(onEverySecond, 1000);
}
});
///////////////////////////
// end of onCreate
/////////////////////////
// in ye joor timer hast ke har sanie yek bar seekbar ro update mikone
return rootView;
}
}
// ########## mediaplayer ro start mikonim mediaplayer.start();
دوسته من شما این قسمت کدت پلی کردی مشکلت اینجاست
سلام
بازم مدیاپلیر....خیلی نافرمه این مدیاپلیر :| بنظرم اینکارو بکن:
اول این کد رو پاک کن:
// ########## mediaplayer ro start mikonim mediaplayer.start();
بعد این قسمت رو تغییر بده:
//dokme play
//shart_barrasi_ejra
boolean mediaPlayerPlayed = false;
play_btn.setOnClickListener(newView.OnClickListener(){privateRunnable onEverySecond;@Overridepublicvoid onClick(View view){
if(mediaPlayerPlayed){
mediaplayer.start();
}else{
mediaPlayerPlayed = true;
mediaplayer =MediaPlayer.create(getActivity(), R.raw.playmusic);
mediaplayer.start();
} // tanzim e dobareye seekbar bad az inke dobare play kardim seekBar.setMax(mediaplayer.getDuration()); seekBar.postDelayed(onEverySecond,1000);}});
امیدوارم مشکلت حل شه :)
باز یه مشکل جدید خوردم تو این کد بالا هر چی رو دکمه استارت میزنم اهنگ رو دوباره میخونه اهنگ انگار رو هم میفته :(
if (mediaplayer.isPlaying())
{
mp.pause();
}
else
{
mp.start();
}
public class PlayerActivity extends Activity implements View.OnClickListener {
static MediaPlayer mp;
Animation fad_in , fade_out;
ArrayList<Integer> mySongs;
SeekBar sb;
Button btPlay,btFF,btFB,btNext,btPV;
int position;
int iv;
Thread updateSeekBar;
int repeat=0;
TextView mylink_bazar,my_telegram;
int entekhab;
Animation anim,anim2,anim3,anim4,anim5,anim6,anim7,anim8;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
TypefaceUtil.overrideFont(PlayerActivity.this, "SERIF", "font/MRT.ttf");
mylink_bazar= (TextView) findViewById(R.id.img5);
my_telegram= (TextView) findViewById(R.id.img1);
String text ="<a href=\"http://cafebazaar.ir/app/?id=ramin.parse.mytest4&ref=share\">دانلود برنامه هفتگی از بازار کلیک اینجا</a>";
mylink_bazar.setMovementMethod(LinkMovementMethod.getInstance());
mylink_bazar.setText(Html.fromHtml(text));
String text1 ="<a href=\"http://telegram.me/raminparseh\">ارتباط با برنامه نویس کلیک اینجا</a>";
my_telegram.setMovementMethod(LinkMovementMethod.getInstance());
my_telegram.setText(Html.fromHtml(text1));
sb= (SeekBar) findViewById(R.id.seekBar_Player);
updateSeekBar = new Thread()
{
@Override
public void run() {
int totalDuration = mp.getDuration();
int currentPosition = 0;
while (mp!=null && currentPosition<totalDuration)
{
try
{
sleep(1000);
currentPosition = mp.getCurrentPosition();
sb.setProgress(currentPosition);
}
catch (InterruptedException e)
{
e.printStackTrace();
return;
}
catch (Exception e)
{
return;
}
}
}
};
if(mp != null)
{
mp.stop();
mp.release();
entekhab=0;
}
Intent i = getIntent();
Bundle b = i.getExtras();
mySongs = (ArrayList) b.getParcelableArrayList("songlist");
position = b.getInt("pos",0);
iv = mySongs.get(position);
mp = MediaPlayer.create(PlayerActivity.this,iv);
////
mp.start();
mp.setLooping(true);
entekhab=1;
sb.setMax(mp.getDuration());
updateSeekBar.start();
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mp.seekTo(seekBar.getProgress());
}
});
}
@Override
public void onClick(View view)
{
int id = view.getId();
switch (id)
{
case R.id.btplay:
if (mp.isPlaying())
{
anim= AnimationUtils.loadAnimation(getApplicationContext(), R.anim.my_click_botton);
view.startAnimation(anim);
mp.pause();
view.setBackgroundResource(R.drawable.zplay);
entekhab=0;
}
else
{
anim2= AnimationUtils.loadAnimation(getApplicationContext(), R.anim.my_click_botton);
view.startAnimation(anim2);
mp.start();
view.setBackgroundResource(R.drawable.zpause);
entekhab=1;
}
break;
case R.id.btff:
anim3= AnimationUtils.loadAnimation(getApplicationContext(), R.anim.my_click_botton);
view.startAnimation(anim3);
mp.seekTo(mp.getCurrentPosition()+5000);
break;
case R.id.btfb:
anim4= AnimationUtils.loadAnimation(getApplicationContext(), R.anim.my_click_botton);
view.startAnimation(anim4);
mp.seekTo(mp.getCurrentPosition()-5000);
break;
}
}
public boolean saveAs(int type, String myMp3, int adress,String tosti) {
byte[] buffer = null;
InputStream fIn = getBaseContext().getResources().openRawResource(adress);
int size = 0;
try {
size = fIn.available();
buffer = new byte[size];
fIn.read(buffer);
fIn.close();
} catch (IOException e) {
return false;
}
String path = Environment.getExternalStorageDirectory().getPath()
+ "/media/audio/ringtones/";
String fileName = myMp3 + ".mp3";
boolean exists = (new File(path)).exists();
if (!exists) {
new File(path).mkdirs();
}
FileOutputStream save;
try {
save = new FileOutputStream(path + fileName);
save.write(buffer);
save.flush();
save.close();
} catch (FileNotFoundException e) {
return false;
} catch (IOException e) {
return false;
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path + fileName)));
File k = new File(path, fileName);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, fileName);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
if (RingtoneManager.TYPE_RINGTONE == type) {
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
} else if (RingtoneManager.TYPE_NOTIFICATION == type) {
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
} else if (RingtoneManager.TYPE_ALARM == type) {
values.put(MediaStore.Audio.Media.IS_ALARM, true);
}
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
Uri newUri = PlayerActivity.this.getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(PlayerActivity.this, type, newUri);
this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);
toast(tosti);
return true;
public void toast(String text)
{
Toast.makeText(PlayerActivity.this, text, Toast.LENGTH_SHORT).show();
}
public void setRingtone(View view) {
anim7= AnimationUtils.loadAnimation(getApplicationContext(), R.anim.my_click_botton);
view.startAnimation(anim7);
int adress = mySongs.get(position);;
String a = mySongs.get(position).toString();
String tosti = "به عنوان زنگ انتخاب شد";
saveAs(RingtoneManager.TYPE_RINGTONE, a, adress,tosti);
}
public void setNotifi(View view) {
anim8= AnimationUtils.loadAnimation(getApplicationContext(), R.anim.my_click_botton);
view.startAnimation(anim8);
int adress = mySongs.get(position);;
String a = mySongs.get(position).toString();
String tosti = "به عنوان نوتیفیکیشن انتخاب شد";
saveAs(RingtoneManager.TYPE_NOTIFICATION, a, adress,tosti);
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
finish();
return true;
}
return false;
}
}
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .
