import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
Oset oset = new Oset();
oset.setont(new onTest(){
public void getx(){
System.out.println("----------------interface Callback---------------");
}
});
try{
Thread.sleep(1000);
}catch(Exception e){
}
oset.methods();
}
}
interface onTest{
public void getx();
}
class Oset {
private onTest ont;
public void setont(onTest ont){
this.ont = ont;
}
public void methods(){
System.out.println("--------------methods------------------");
ont.getx();
}
}
Demo
https://paiza.io/projects/uWqAgbv37VXi3LEWkS4WlQ
2017年4月24日月曜日
2017年4月23日日曜日
Thread.activeCount()
Thread.activeCount()
返回当前线程的线程组中活动线程的数目。
public class T2 {
public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
while (true) {
// System.out.println(1);
}
}
}).start();
new Thread(new Runnable() {
public void run() {
while (true) {
// System.out.println(2);
}
}
}).start();
new Thread(new Runnable() {
public void run() {
while (true) {
// System.out.println(3);
}
}
}).start();
System.out.println(Thread.activeCount());
}
}
2017年4月11日火曜日
Java ArrayList to String[]
Java ArrayList to String[]
https://paiza.io/projects/3F_iZEjxHh9AUNgPwAohxA
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) throws Exception {
ArrayList<String> sl = new ArrayList<String>();
sl.add("a");
sl.add("b");
sl.add("c");
String[] temp = sl.toArray(new String[]{});
System.out.println(temp.length);
for( String x:temp){
System.out.println(x);
}
}
}
https://paiza.io/projects/3F_iZEjxHh9AUNgPwAohxA
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) throws Exception {
ArrayList<String> sl = new ArrayList<String>();
sl.add("a");
sl.add("b");
sl.add("c");
String[] temp = sl.toArray(new String[]{});
System.out.println(temp.length);
for( String x:temp){
System.out.println(x);
}
}
}
2017年4月6日木曜日
Android Intent 文件存储 找到文件
Intent 文件存储 找到文件
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
protected void t1() {
int cc = 100;
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, cc);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && resultCode == RESULT_OK) {
Uri uri = data.getData();
System.out.println(uri);
Fpath(uri);
}
}
protected void Fpath(Uri uri) {
ContentResolver contentResolver = getContentResolver();
long id = ContentUris.parseId(uri);
// 1. select _id,_data from uri while _id = id;
String[] select = new String[]{"_id","_data"};
Cursor cursor = contentResolver.query(uri, select, "_id = " + id, null, null);
// 2. select * from uri while _id = id;
// Cursor cursor = contentResolver.query(uri, null, "_id = " + id, null, null);
// 3. select * from uri;
// Cursor cursor = contentResolver.query(uri, null, null, null, null);
while (cursor.moveToNext()) {
int cont = cursor.getColumnCount();
for (int i = 0; i < cont; i++) {
System.out.print(cursor.getColumnName(i) + ">> ");
System.out.println(cursor.getString(i));
}
}
}
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
protected void t1() {
int cc = 100;
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, cc);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && resultCode == RESULT_OK) {
Uri uri = data.getData();
System.out.println(uri);
Fpath(uri);
}
}
protected void Fpath(Uri uri) {
ContentResolver contentResolver = getContentResolver();
long id = ContentUris.parseId(uri);
// 1. select _id,_data from uri while _id = id;
String[] select = new String[]{"_id","_data"};
Cursor cursor = contentResolver.query(uri, select, "_id = " + id, null, null);
// 2. select * from uri while _id = id;
// Cursor cursor = contentResolver.query(uri, null, "_id = " + id, null, null);
// 3. select * from uri;
// Cursor cursor = contentResolver.query(uri, null, null, null, null);
while (cursor.moveToNext()) {
int cont = cursor.getColumnCount();
for (int i = 0; i < cont; i++) {
System.out.print(cursor.getColumnName(i) + ">> ");
System.out.println(cursor.getString(i));
}
}
}
2017年4月5日水曜日
Android MediaPlayer 播放指定文件夾
Android MediaPlayer 播放指定文件夾
public void PlayList(File file) {
if (file.isFile()) {
if (file.isFile()) {
fileDir = file.getParentFile();
fileName = file.getName();
} else {
fileDir = file;
fileName = null;
}
fileArray = new ArrayList<String>();
fileDir.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if (name.endsWith(".mp3") || name.endsWith(".mp4") || name.endsWith(".wav")) {
if ((new File(dir, name)).isFile()) {
fileArray.add(name);
return true;
} else {
return false;
}
} else {
return false;
}
}
});
PlayFile(fileName);
} else {
Log.i(TAG, "nofindthefile...................................................");
}
}
public void PlayFile(final String fileName) {
Log.i(TAG, "FileName" + fileName);
intNum = fileArray.indexOf(fileName);
if (intNum == -1) {
intNum = 0;
}
Uri uri = Uri.fromFile(new File(fileDir, fileArray.get(intNum)));
if (intNum < fileArray.size() - 1) {
intNum++;
} else {
intNum = 0;
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(context, uri);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
PlayFile(fileArray.get(intNum));
}
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
PlayFile(fileArray.get(intNum));
}
});
}
public void PlayList(File file) {
if (file.isFile()) {
if (file.isFile()) {
fileDir = file.getParentFile();
fileName = file.getName();
} else {
fileDir = file;
fileName = null;
}
fileArray = new ArrayList<String>();
fileDir.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if (name.endsWith(".mp3") || name.endsWith(".mp4") || name.endsWith(".wav")) {
if ((new File(dir, name)).isFile()) {
fileArray.add(name);
return true;
} else {
return false;
}
} else {
return false;
}
}
});
PlayFile(fileName);
} else {
Log.i(TAG, "nofindthefile...................................................");
}
}
public void PlayFile(final String fileName) {
Log.i(TAG, "FileName" + fileName);
intNum = fileArray.indexOf(fileName);
if (intNum == -1) {
intNum = 0;
}
Uri uri = Uri.fromFile(new File(fileDir, fileArray.get(intNum)));
if (intNum < fileArray.size() - 1) {
intNum++;
} else {
intNum = 0;
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(context, uri);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
PlayFile(fileArray.get(intNum));
}
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
PlayFile(fileArray.get(intNum));
}
});
}
ラベル:
Android
2017年4月2日日曜日
Java FilenameFilter
Java FilenameFilter
public void test() {
File file = new File(Environment.getExternalStorageDirectory().toString());
String[] files = file.list(new MusicFilter());
if (files.length > 0) {
for (String name : files) {
System.out.println(name);
}
}
String[] files2 = file.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if (name.endsWith(".mp3") || name.endsWith(".wav")) {
return true;
} else {
return false;
}
}
});
if (files2.length > 0) {
for (String name : files2) {
System.out.println(name);
}
}
}
public class MusicFilter implements FilenameFilter {
@Override
public boolean accept(File dir, String name) {
if (name.endsWith(".mp3") || name.endsWith(".wav")) {
return true;
} else {
return false;
}
}
}
public void test() {
File file = new File(Environment.getExternalStorageDirectory().toString());
String[] files = file.list(new MusicFilter());
if (files.length > 0) {
for (String name : files) {
System.out.println(name);
}
}
String[] files2 = file.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if (name.endsWith(".mp3") || name.endsWith(".wav")) {
return true;
} else {
return false;
}
}
});
if (files2.length > 0) {
for (String name : files2) {
System.out.println(name);
}
}
}
public class MusicFilter implements FilenameFilter {
@Override
public boolean accept(File dir, String name) {
if (name.endsWith(".mp3") || name.endsWith(".wav")) {
return true;
} else {
return false;
}
}
}
2017年3月31日金曜日
Android 卡拉OK
Android 卡拉OK
protected void arp() {
int sampleRateInHz = 44100;
int rec_bufsize = AudioRecord.getMinBufferSize(sampleRateInHz, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
int play_bufsize = AudioTrack.getMinBufferSize(sampleRateInHz, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
// MediaRecorder.AudioSource.MIC
AudioRecord audioRecord = new AudioRecord(
MediaRecorder.AudioSource.MIC,
sampleRateInHz,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
rec_bufsize);
AudioTrack audioTrack = new AudioTrack(
AudioManager.STREAM_MUSIC,
sampleRateInHz,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT,
play_bufsize,
AudioTrack.MODE_STREAM);
byte[] temp = new byte[16];
audioRecord.startRecording();
audioTrack.play();
while (!stop) {
audioRecord.read(temp, 0, temp.length);
audioTrack.write(temp, 0, temp.length);
}
audioTrack.stop();
audioRecord.stop();
audioRecord.release();
}
protected void arp() {
int sampleRateInHz = 44100;
int rec_bufsize = AudioRecord.getMinBufferSize(sampleRateInHz, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
int play_bufsize = AudioTrack.getMinBufferSize(sampleRateInHz, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
// MediaRecorder.AudioSource.MIC
AudioRecord audioRecord = new AudioRecord(
MediaRecorder.AudioSource.MIC,
sampleRateInHz,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
rec_bufsize);
AudioTrack audioTrack = new AudioTrack(
AudioManager.STREAM_MUSIC,
sampleRateInHz,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT,
play_bufsize,
AudioTrack.MODE_STREAM);
byte[] temp = new byte[16];
audioRecord.startRecording();
audioTrack.play();
while (!stop) {
audioRecord.read(temp, 0, temp.length);
audioTrack.write(temp, 0, temp.length);
}
audioTrack.stop();
audioRecord.stop();
audioRecord.release();
}
ラベル:
Android
登録:
投稿 (Atom)