2012-03-07 65 views
0

我是一個noob,我在停止我的應用程序中的音樂時遇到問題,它會在關閉應用程序後繼續播放很長時間。繼承人一些代碼,我希望有人可以幫助!以下是我需要幫助的部分,其餘代碼有效。卡住後如何使活動停止播放鈴聲

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.content.res.AssetFileDescriptor; 
import android.content.res.Resources; 
import android.graphics.Typeface; 

import android.media.MediaPlayer; 
import android.media.RingtoneManager; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.view.ContextMenu; 

import android.view.MenuItem; 
import android.view.View; 
import android.view.ContextMenu.ContextMenuInfo; 

import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

public class Tones extends Activity { 
    int selectedSoundId; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tones); 

     final MediaPlayer player = new MediaPlayer(); 
     final Resources res = getResources(); 

     // Keep them in the same order 
     final int[] buttonIds = { R.id.s1, R.id.s2, 
       R.id.s3, R.id.s4, R.id.s5, 
       R.id.s6, R.id.s7, 
       R.id.s8, R.id.s9, R.id.s10, 
       R.id.s11, R.id.s12, R.id.s13, 
       R.id.14, R.id.s15 }; 
     final int[] soundIds = { R.raw.s1, R.raw.s2, 
       R.raw.s3, R.raw.s4, R.raw.s5, 
       R.raw.s6, R.raw.s7, 
       R.raw.s8, R.raw.s9, R.raw.s10, 
       R.raw.s11, R.raw.s12, R.raw.s13, 
       R.raw.s14, R.raw.s15, }; 


    View.OnClickListener listener = new View.OnClickListener() { 

      public void onClick(View v) { 
       // find the index that matches the button's ID, and then reset 
       // the MediaPlayer instance, set the data source to the 
       // corresponding 
       // sound effect, prepare it, and start it playing. 
       for (int i = 0; i < buttonIds.length; i++) { 
        if (v.getId() == buttonIds[i]) { 
         selectedSoundId = soundIds[i]; 
         AssetFileDescriptor afd = res 
           .openRawResourceFd(soundIds[i]); 
         player.reset(); 
         try { 
          player.setDataSource(afd.getFileDescriptor(), 
            afd.getStartOffset(), afd.getLength()); 
         } catch (IllegalArgumentException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } catch (IllegalStateException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
         try { 
          player.prepare(); 
         } catch (IllegalStateException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
         player.start(); 

        } 

        else if (v.getId() == buttonIds[i]) { 
         selectedSoundId = soundIds[i]; 
         player.pause(); 
         finish(); 



        } 
       } 
      } 
     }; 

     // set the same listener for every button ID, no need 
     // to keep a reference to every button 
     for (int i = 0; i < buttonIds.length; i++) { 
      Button soundButton = (Button) findViewById(buttonIds[i]); 
      registerForContextMenu(soundButton); 
      soundButton.setOnClickListener(listener); 

     } 

    } 

    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, 
      ContextMenuInfo menuInfo) { 
     super.onCreateContextMenu(menu, v, menuInfo); 
     menu.setHeaderTitle("Save As..."); 
     menu.add(0, v.getId(), 0, "Ringtone"); 
     menu.add(0, v.getId(), 0, "Notification"); 
    } 

    @Override 
    public boolean onContextItemSelected(MenuItem item) { 
     if (item.getTitle() == "Ringtone") { 
      function1(item.getItemId()); 
     } else if (item.getTitle() == "Notification") { 
      function2(item.getItemId()); 
     } else { 
      return false; 
     } 
     return true; 
    } 

    public void function1(int id) { 

     if (savering(selectedSoundId)) { 
      // Code if successful 
      Toast.makeText(this, "Saved as Ringtone", Toast.LENGTH_SHORT) 
        .show(); 
     } else { 
      // Code if unsuccessful 
      Toast.makeText(this, "Failed - Check your SDCard", 
        Toast.LENGTH_SHORT).show(); 
     } 

    } 

    public void function2(int id) { 
     if (savenot(selectedSoundId)) { 
      // Code if successful 
      Toast.makeText(this, "Saved as Notification", Toast.LENGTH_SHORT) 
        .show(); 
     } else { 
      // Code if unsuccessful 
      Toast.makeText(this, "Failed - Check your SDCard", 
        Toast.LENGTH_SHORT).show(); 
     } 
    } 

    // Save into Ring tone Folder 

    public boolean savering(int ressound) { 
     byte[] buffer = null; 
     InputStream fIn = getBaseContext().getResources().openRawResource(
       ressound); 
     try { 
      buffer = new byte[fIn.available()]; 
      fIn.read(); 
      fIn.close(); 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      return false; 
     } 

     String path = Environment.getExternalStorageDirectory().getPath() 
       + "/media/ringtone/"; 

     String filename = "sounds1" + selectedSoundId + ".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) { 
      // TODO Auto-generated catch block 
      return false; 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      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, selectedSoundId); 
     values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3"); 
     values.put(MediaStore.Audio.Media.ARTIST, "sounds"); 
     values.put(MediaStore.Audio.Media.IS_RINGTONE, true); 
     values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false); 
     values.put(MediaStore.Audio.Media.IS_ALARM, true); 
     values.put(MediaStore.Audio.Media.IS_MUSIC, false); 

     Uri uri = MediaStore.Audio.Media.getContentUriForPath(k 
       .getAbsolutePath()); 
     getContentResolver().delete(
       uri, 
       MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() 
         + "\"", null); 
     Uri newUri = getContentResolver().insert(uri, values); 
     RingtoneManager.setActualDefaultRingtoneUri(this, 
       RingtoneManager.TYPE_RINGTONE, newUri); 

     return true; 
    } 

    // Save in Notification Folder 

    public boolean savenot(int ressound) { 
     byte[] buffer = null; 
     InputStream fIn = getBaseContext().getResources().openRawResource(
       ressound); 
     int size = 0; 

     try { 
      size = fIn.available(); 
      buffer = new byte[size]; 
      fIn.read(buffer); 
      fIn.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      return false; 
     } 

     String path = Environment.getExternalStorageDirectory().getPath() 
       + "/media/notification/"; 

     String filename = "sounds" + selectedSoundId + ".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) { 
      // TODO Auto-generated catch block 
      return false; 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      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, selectedSoundId); 
     values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3"); 
     values.put(MediaStore.Audio.Media.ARTIST, "sounds"); 
     values.put(MediaStore.Audio.Media.IS_RINGTONE, false); 
     values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true); 
     values.put(MediaStore.Audio.Media.IS_ALARM, true); 
     values.put(MediaStore.Audio.Media.IS_MUSIC, false); 

     Uri uri = MediaStore.Audio.Media.getContentUriForPath(k 
       .getAbsolutePath()); 
     getContentResolver().delete(
       uri, 
       MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() 
         + "\"", null); 
     Uri newUri = getContentResolver().insert(uri, values); 
     RingtoneManager.setActualDefaultRingtoneUri(this, 
       RingtoneManager.TYPE_NOTIFICATION, newUri); 

     return true; 
+0

你試過重寫'onPause','你的活動onDestroy'和禁用/在那裏卸下球員? – 2012-03-07 03:34:31

+0

我會在那裏放?我已經嘗試在我的活動底部放置一個onDestroy,但它不起作用。我有上面的更新代碼,如果你可以檢查它會很好 – nlitech 2012-03-07 14:24:04

回答

0
//Give path of your media file 

     String path=""; 

      MediaPlayer Uri myUri = Uri.parse(audPath[songIndex]); 

     mp=new MediaPlayer(); 
      mp.setLooping(false); 
     mp = MediaPlayer.create(ActivityName.this, myUri); 

     mp.start(); 



     //stop music file on completion 
     mp.setOnCompletionListener(new OnCompletionListener() { 

          @Override 
          public void onCompletion(MediaPlayer mp) { 
           // TODO Auto-generated method stub 
           mp.stop(); 
           mp.release(); 
          } 
         }); 
+0

Pradeep的musicfileId我很困惑,至於放什麼,這是我的代碼的頂部,我有一個字符串的所有聲音和標題。 – nlitech 2012-03-07 12:55:28