package com.Aina.Android; import java.io.IOException; import android.content.Context; import android.media.MediaPlayer; public class MIDIPlayer { private Context mContext = null; private MediaPlayer mMediaPlayer = null; public MIDIPlayer(Context context){ mContext = context; } public void start(){ mMediaPlayer = MediaPlayer.create(mContext, R.raw.start);//装载资源 mMediaPlayer.setLooping(true);//循环播放 try { mMediaPlayer.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } mMediaPlayer.start(); } public void stop(){ if(mMediaPlayer != null){ mMediaPlayer.stop(); mMediaPlayer.release(); } } } package com.Aina.Android; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.TextView; public class Test_File extends Activity { /** Called when the activity is first created. */ private MIDIPlayer mMIDIPlayer = null; private boolean bm = false; private TextView tv = null; private Button btn = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mMIDIPlayer = new MIDIPlayer(this); tv = (TextView) this.findViewById(R.id.TextView); btn = (Button) this.findViewById(R.id.Button01); this.load(); if (bm) { mMIDIPlayer.start(); tv.setText("当前音效:开"); } else { mMIDIPlayer.stop(); tv.setText("当前音效:关"); } btn.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Test_File.this, sg.class); Test_File.this.startActivity(intent); } }); } @Override protected void onPause() { this.save(); if (bm) { mMIDIPlayer.stop(); } super.onPause(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_DPAD_UP) { mMIDIPlayer.start(); tv.setText("当前音效:开"); bm = true; } else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) { mMIDIPlayer.stop(); tv.setText("当前音效:关"); bm = false; } return super.onKeyDown(keyCode, event); } // 读取数据 private void load() { Properties pro = new Properties(); try { FileInputStream fin = this.openFileInput("music.cfg");// 读取文件 try { pro.load(fin);// 加载到Properties属性文件里面 } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } bm = Boolean.valueOf(pro.getProperty("isopen", "false")); } private String read(Context context, String filepath) { String data = ""; StringBuffer sb = new StringBuffer(); int ch = 0; try { FileInputStream in = context.openFileInput(filepath); while ((ch = in.read()) != -1) { sb.append((char) ch); } in.close(); in = null; data = sb.toString(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return data; } // 保存数据 private void save() { Properties pro = new Properties(); pro.put("isopen", String.valueOf(bm)); try { FileOutputStream fout = this.openFileOutput("music.cfg", Activity.MODE_WORLD_READABLE | Activity.MODE_WORLD_WRITEABLE); pro.store(fout, "");// 写入输出流 } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private void write(Context context, String filepath, String msg) { try { FileOutputStream out = context.openFileOutput(filepath, context.MODE_WORLD_WRITEABLE); out.write(msg.getBytes()); out.flush(); out.close(); out = null; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }